import java.util.*;

public class Test {

    public static List<Integer> findSubstring(String s, String[] words) {
        List<Integer> list = new ArrayList<>();
        Map<String,Integer> hash1 = new HashMap<>();
        Map<String,Integer> hash2 = new HashMap<>();
        for(String x : words) {
            hash2.put(x,hash2.getOrDefault(x,0) + 1);
        }

        int gap = words[0].length();
        int size = words.length;//有效单词个数
        int count = 0;//统计有效单词数量
        for(int i = 0; i < gap; i++) {
            count = 0;
            for(int left = i, right = left + gap - 1; right < s.length(); right += gap) {
                String x = s.substring(right - gap + 1, right + 1);
                //进窗口
                hash1.put(x,hash1.getOrDefault(x,0) + 1);
                if(hash1.get(x) <= hash2.getOrDefault(x,0)) {
                    count++;
                }

                //出窗口
                if(right - left + 1 > size * gap) {
                    x = s.substring(left, left + gap);
                    if(hash1.get(x) <= hash2.getOrDefault(x, 0)) {
                        count--;
                    }
                    hash1.put(x,hash1.get(x) - 1);
                    left += gap;
                }

                //更新数据
                if(count == size) {
                    list.add(left);
                }
            }
        }

        return list;
    }
    public static void main(String[] args) {
        LinkedHashMap<Integer,Integer> linkedHashMap = new LinkedHashMap<>();
        String s = "a";
        String[] words = {"aa","aa"};
        List<Integer> list = findSubstring("aaaaaaaaaaaaaa", words);
        System.out.println(list);
    }
}
